import { defineStore } from 'pinia'
import type { ICollageGoods } from '@/types/type'

export const getcollageStatus = defineStore('collageStatus', {
    state() {
        return {
            //定义在拼团页面的变量，商品数据数组
            paymentGoods: [] as Array<ICollageGoods>,
            stateAll: false
        }
    },
    actions: {
        checkAll() {
            this.stateAll = !this.stateAll
            this.paymentGoods.forEach(item => (item.state = this.stateAll))
        },
        singleCheck(id: number) {
            const goods: ICollageGoods | undefined = this.paymentGoods.find(
                item => item.id === id
            )
            if (goods) {
                goods.state = !goods.state
            }
            this.stateAll = this.paymentGoods.every(item => item.state)
        },
       
        // 保存在store里面，拼团状态里面使用
        saveCollageGoods(collageStatus: ICollageGoods) {
            //把接收传的collectionGoods实参，然后把它赋给collectionList
            this.paymentGoods.push(collageStatus)
        },
        deleteGoods(id: number) {
            const index = this.paymentGoods.findIndex(item => item.id === id)
            this.paymentGoods.splice(index, 1)
        },
        deleteAllGoods() {
            this.paymentGoods.splice(0, this.paymentGoods.length)
        }

    },
    getters: {

    },
    persist: {
        enabled: true,//允许存储,默认true
        strategies: [
            {
                key: 'collageStatus',//存储的数据名字
                storage: localStorage,//保存的类型，默认是session,
                paths: ['collageStatus'],//需要持久保存的state里面的数据
            },
        ],
    }
})
